﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;

/// <summary>
///保存视频相关信息，实现视频操作的类
///视频的目录（即分类）使用Index保存直接上级目录，通过数据库或者xml文档保存分类信息
///对视频的搜索使用tags，tags标志了此视频，默认把视频名称作为一个tags。
/// 
/// </summary>
public class Movie
{

    private int movieID;//视频编号，由数据库自动生成。
    private String movieName=null;
    private String ownerID=null;//上传者ID
    private String introduction=null;//视频简介
    private String url = null;//视频文件保存路径
    private String[] catalogs;//索引，即视频的上级目录
    private String[] tag=null;//标签，搜索使用关键字。每个视频最多有6个Key Word。
    private DateTime uploadDatetime;//上传时间
 
    private int focus=0;//关注次数，此视频的所有点击次数。

    public Movie() { }

    //索引数组转索引字符串
    public String toCatalog(String[] catalogs)
    {
        String str = "";
        for (int i = 0; i < catalogs.Length - 1; i++)
        {
            str += catalogs[i] + ",";//用","来分隔索引
        }
        str += catalogs[catalogs.Length - 1];
        return str;
    }

    //索引字符串转索引数组
    //public string[] separateCatalog(string catalog)
    //{
    //    string[] catalogs = catalog.Split(',');
    //    return catalogs;
    //}

    /// <summary>
    /// 向数据库添加影片记录
    /// </summary>
    /// <param name="movieName"></param>
    /// <param name="ownerID"></param>
    /// <param name="introduction"></param>
    /// <param name="url"></param>
    /// <param name="catalogs"></param>
    /// <param name="tag"></param>
    /// <returns></returns>
    public bool Add(String movieName, String ownerID, String introduction, String url, String[] catalogs, String[] tag)
    {
        String catalog = toCatalog(catalogs);
        uploadDatetime = DateTime.Now;
        focus = 0;
        String sql="insert into tb_Movie(movieName,ownerID,introduction,url,catalogs,tags,uploadDatetime,focus) values('"
            + movieName + "','" + ownerID + "','" + introduction + "','" + url + "','" + catalog + "','" + string.Join(",", tag) + "','" + uploadDatetime.ToString() + "'," + focus.ToString() + " )";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd(sql);
    }

    //删除影片
    public bool Delete(int movieID)
    {
        Movie movie = new Movie();
        movie.movieID = movieID;
        if (movie.Load())//先加载影片信息
        {
            String sql = "delete from tb_Movie where movieID=" + movieID.ToString();
            SqlExecutor myExec = new SqlExecutor();
            if (myExec.UpdateCmd(sql))//先删除数据库记录
            {
                try
                {
                    if (File.Exists(movie.url))
                    {
                        File.Delete(movie.url);
                    }
                    return true;
                }
                catch { return true; }
            }
            else { return false; }
        }
        else { return false; }
    }

    //更新视频信息
    public bool Update(int movieID, String movieName, String introduction, String[] catalogs, String[] tag)
    {
        this.movieName = movieName;
        this.introduction = introduction;
        this.catalogs = catalogs;
        this.tag = tag;

        String catalog = toCatalog(catalogs);
        String sql = "update tb_Movie set movieName='" + movieName + "',introduction='" + introduction + "',catalogs='"
            + catalog + "',tags='" + string.Join(",",tag) + "' where movieID=" + movieID.ToString() + "";
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd(sql);
    }

    //视频人气
    public bool UpMovie(int movieID)
    {
        SqlExecutor myExec = new SqlExecutor();
        return myExec.UpdateCmd("update tb_movie set focus=focus+1 where movieID=" + movieID.ToString() + "");
    }

    /// <summary>
    /// 从数据库加载影片信息
    /// </summary>
    /// <returns></returns>
    public bool Load()
    {
        string sql = "select * from tb_Movie where movieID=" + movieID.ToString();
        SqlExecutor myExec = new SqlExecutor();
        DataTable movieTable = myExec.SelectCmd(sql);
        if (movieTable.Rows.Count == 0) { return false; }
        this.movieName = (string)movieTable.Rows[0]["movieName"];
        this.ownerID = (string)movieTable.Rows[0]["ownerID"];
        this.uploadDatetime = (DateTime)movieTable.Rows[0]["uploadDatetime"];
        this.introduction = (string)movieTable.Rows[0]["introduction"];
        this.catalogs = ((string)movieTable.Rows[0]["catalogs"]).Split(',');
        this.tag = ((string)movieTable.Rows[0]["tags"]).Split(',');
        this.focus = (int)movieTable.Rows[0]["focus"];
        this.url = (string)movieTable.Rows[0]["url"];
        return true;
    }

    //影片查询函数
    static public DataTable Search(string catalogs)
    {
        string cmd = "select * from tb_Movie ";
        bool isFirst = true;
        if (isFirst)
        {
            cmd += "where catalogs like '%" + catalogs + "%' ";
            isFirst = false;
        }
        else
        {
            cmd += "and catalogs like '%" + catalogs + "%' ";
        }
        SqlExecutor myExec = new SqlExecutor();
        return myExec.SelectCmd(cmd);
    }

    public int MovieID
    {
        get{return movieID;}
        set { movieID = value; }
    }
    
    public String MovieName
    {
        set{movieName=value;}
        get{return movieName;}
    }

    public String OwnerID
    {
        set{ownerID=value;}
        get{return ownerID;}
    }

    public String Introduction
    {
        set{introduction=value;}
        get{return introduction;}
    }


    public String URL
    {
        set{url=value;}
        get{return url;}
    }

    public String[] Catalogs
    {
        set { catalogs = value; }
        get { return catalogs; }
    }

    public String[] Tag
    {
        set{tag=value;}
        get{return tag;}
    }

    public DateTime UploadDatetime
    {
        set { uploadDatetime = value; }
        get { return uploadDatetime; }
    }

    public int Focus
    {
        set{focus=value;}
        get{return focus;}
    }


}